package org.me.SendSMS.adapter;

import java.util.List;

import org.me.SendSMS.R;
import org.me.SendSMS.SendSMSActivity;
import org.me.SendSMS.data.MessageType;
import org.me.SendSMS.utli.GlobalVariable;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MessageTypeAdapter  extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private Context mContext;
	private List<MessageType> listMessage;
	
	
	public MessageTypeAdapter(Context context, List<MessageType> list)
	{
		mContext 	= context;
		mInflater 	= LayoutInflater.from(context);
		listMessage = list;
       
	}
	
	public class ViewHolder
	{
		TextView txtTitle;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listMessage.size();
}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub

		final ViewHolder holder;

		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.items_message_type, null);
			
			holder.txtTitle 		= (TextView)view.findViewById(R.id.txtTinNhanType);
			
			view.setTag(holder);
		}
		
		else
		{
			holder = (ViewHolder) view.getTag();
		}

		
		holder.txtTitle.setTag(position);
		
		//RemoveSign4VietnameseString(listMessage.get(position).GetTitle())
		if(GlobalVariable.isVietNamese)
			holder.txtTitle.setText(listMessage.get(position).GetTitle());
		else
			holder.txtTitle.setText(RemoveSign4VietnameseString(listMessage.get(position).GetTitle()));
		
		holder.txtTitle.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				IsSendSMS(holder.txtTitle.getText().toString());
				return false;
			}
		});
		
		holder.txtTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext,
						"Press long click to select action !",
						Toast.LENGTH_LONG).show();	
			}
		});
		
		
		return view;
	}
	
	public static String RemoveSign4VietnameseString(String str)

	 {
	  // Tiến hành thay thế , lọc bỏ dấu cho chuỗi

	  for (int i = 1; i < GlobalVariable.VietnameseSigns.length; i++) {
	   for (int j = 0; j < GlobalVariable.VietnameseSigns[i].length(); j++) {
	    str = str.replace(GlobalVariable.VietnameseSigns[i].charAt(j),
	    		GlobalVariable.VietnameseSigns[0].charAt(i - 1));
	   }
	  }
	  return str;
	 }
	
	public void IsSendSMS(final String message){
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setMessage("Select action ?")
		       .setCancelable(false)
		       .setPositiveButton("Send SMS now", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	  
		        	   GlobalVariable.HIDDEN 	= false;
		        	   Intent myIntent 			= new Intent(mContext, SendSMSActivity.class);
		        	   myIntent.putExtra("message", message);
		        	   myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        	   mContext.startActivity(myIntent);
		        	   

		           }
		       })
		       .setNegativeButton("Select SMS for timer", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   GlobalVariable.HIDDEN 	= true;
		        	   Intent myIntent 			= new Intent(mContext, SendSMSActivity.class);
		        	   myIntent.putExtra("message", message);
		        	   myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        	   mContext.startActivity(myIntent);
		           }
		       });
		AlertDialog alert = builder.create();			
		alert.show();
	}
}
